package com.cloudptt.api.product.func.call;

import android.os.Handler;
import android.os.Looper;
import android.os.Message;

public class TimerMeter extends Handler{
//	private static final int MsgId_Start = 1;
	private static final int MsgId_Loop = 1;
	private static final int MsgId_Stop = 2;
	private ISecondChange callBack = null;
	private int sec = 0;
	public TimerMeter(ISecondChange callBack)
	{
		super(Looper.getMainLooper());
		this.callBack = callBack;
		this.sec = 0;
	}
	
	public void startTiming()
	{
		Message msg = this.obtainMessage(MsgId_Loop);
		msg.sendToTarget();
	}
	
	public void stopTiming()
	{
		Message msg = this.obtainMessage(MsgId_Stop);
		msg.sendToTarget();
	}
	
	public void delayedSendMessage()
	{
		Message msg = this.obtainMessage(MsgId_Loop);
		this.sendMessageDelayed(msg, 1000);
	}
	
	@Override
	public void handleMessage(Message msg) {
		// TODO Auto-generated method stub
		switch(msg.what)
		{
		case MsgId_Loop:
			sec ++;
			this.removeMessages(MsgId_Loop);
			delayedSendMessage();
			callBack.secondChange(sec);
			break;
		case MsgId_Stop:
			this.removeMessages(MsgId_Loop);
			break;
		default:
			break;
		}
	}
}
